<!--添加信号-->
<template>
  <div class="all" @viewappear="viewappear" @viewdisappear="viewdisappear">
    
    <scroller class="scroller">
      <div class="wrapper">
        
        <VPInput class="cell" v-model="name" 
        :title="T('信号名称')" :placeholder="T('请输入信号名称')" type="text" />
        
        <VPType class="cell" v-model="pairType" 
        :types="pairTypes" :title="T('交易品种')" 
        @typeChange="pairChange('')" />

        <VPSelect class="cell" v-model="exchange" 
        :selects="exchanges" :title="T('交易所')" 
        valueKey="exchangeName" :leftImg="'v2_'+exchangeMap.exchangeName.toLowerCase()+'.png'"
        @select="pairChange('')" />

        <VPPair v-if="pairType==0" class="cell" ref="pair" :pairMap="pairMap" 
        @message="pairMessage" @pairClick="pairClick" />

        <VPPairC v-else-if="pairType==1" class="cell" ref="pair" :pairMap="pairMap" 
        @message="pairMessage" @pairClick="pairClick" />

        <VPSelect class="cell" v-model="param" 
        :selects="params" :title="T('触发类型')" valueKey="name"
        @select="paramTypeChange" />

        <div v-for="(item, index) in datas" :key="index">
          <VPInput class="cell" v-model="item.value"
          :title="item.name" :placeholder="item.option ? T('选填') : T('请输入'+item.name)" 
          :type="item.valueType" :unit="item.unit" />
        </div>

        <VPSelect class="cell" v-model="opType" 
        :selects="opTypes" :title="T('触发类型')" />
      </div>
    </scroller>

    <!--启动-->
    <div class="up bg3">
      <div class="up-but main1" @click="addClick">
        <text class="font32 text1">{{T('创建信号')}}</text>
      </div>
    </div>
  </div>
</template>

<script>

//功能
import jmodal from "@/_js/modal";
import jmath from "@/_js/math";
import jnavigator from "@/_js/navigator"; 
import jbroadcast from "@/_js/broadcast";
import jhttps from "@/_js/https";

import jSignal from "./view/Signal"

const weexFunction = weex.requireModule('WeexFunction');

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T('Vtrading信号'));

//组件
import VPInput from "@/_public/VPInput.vue";
import VPType from "@/_public/VPType.vue";
import VPSelect from "@/_public/VPSelect.vue";
import VPPair from "@/_public/VPPair.vue";
import VPPairC from "@/_public/VPPairC.vue";
export default {

  components: {
    VPInput,
    VPType,
    VPSelect,
    VPPair,
    VPPairC
  },
  
  data() {
    return {
      T:T,

      name:'', //信号名称

      pairType:0, //交易对品种
      pairTypes:[T('现货'), T('合约')], 
      exchange:0,
      exchanges:[{'exchangeName':'Binance', 'exchangeId':'4'},
                 {'exchangeName':'Huobi', 'exchangeId':'3'},
                 {'exchangeName':'OkexV5', 'exchangeId':'29'}],
      pairMap:undefined,

      opType:0, //触发频率 0表示不限制次数
      opTypes:[T('持续触发'), T('单次触发')],

      
      param:0, //参数
      params: jSignal.getContents(),   
      
      datas:[],
    };
  },

  mounted() {

    this.initDatas();
  },
  
  computed: {

    //交易所对象
    exchangeMap() {
      var exchangeMap = this.exchanges[this.exchange];
      return exchangeMap;
    },

    //当前配置
    paramMap() {
      var param = this.params[this.param];
      return param;
    }
  },

  methods: {
    /*************************************************通信触发*/
    //完全显示
    viewappear() {
      this.$refs.pair && this.$refs.pair.viewappear();
    },

    //完全隐藏
    viewdisappear() {
      this.$refs.pair && this.$refs.pair.viewdisappear();
    },

    /********************************************** 其他*/
    initDatas() {

      var datas = [];
      this.paramMap.array.forEach(map => {

        map = JSON.parse(JSON.stringify(map));
        this.initData(map, map);
        datas.push(map);
      });
      this.datas = datas;
    },

    upDatas() {

      var array = this.paramMap.array;
      for (let index = 0; index < array.length; index++) {
        var map = array[index];
        var data = this.datas[index];
        this.initData(map, data);
      }
    },

    initData(oldMap, newMap) {

      if (oldMap.value == 'market') {
        newMap.value = this.pairMap ? this.pairMap.last : '';
      }

      if (oldMap.unit == 'baseCoin') {
        newMap.unit = this.pairMap ? this.pairMap.baseCoin : '';
      } else if (oldMap.unit == 'counterCoin') {
        newMap.unit = this.pairMap ? this.pairMap.counterCoin : '';
      } 
    },

    /********************************************** https*/
    //https 创建信号
    httpsCreateSignal(){

      jmodal.showLoad();
      var content = JSON.parse(JSON.stringify(this.paramMap));
      content.array = this.datas;

      var contractId = this.pairType ? this.pairMap.contractId : undefined;
      var param = {'signalName': this.name, //信号名称
                    'type': this.pairType ? 'contract_usdt' : 'spot',
                    contractId,
                    'exchangeName':this.exchangeMap.exchangeName, //交易所
                    'counterCoin': this.pairMap.counterCoin, //交易币
                    'baseCoin': this.pairMap.baseCoin, //本币

                    'signalType': 'priceWarn', //信号类型（priceWarn,tradview）
                    'warningCount':this.opType+'', //0持续触发 1单次触发

                    'signalContent': content} //信号内容;
                                
      jhttps.postX("signal/createSignal",param,true,(data)=>{

        jmodal.hideLoad();
        if(data.status == 200){
          jmodal.toast('创建成功');
          jbroadcast.send('SignalAdd');
          jnavigator.back();
        }else{
          jmodal.toast(data.msg);
        }
      });
    },

    /********************************************** 点击*/
    //交易对改变
    pairChange(pair) {


      this.pairMap = pair;
      this.upDatas();
      var map = {
        'pairType':this.pairType,
        'exchange':this.exchange};
      if (pair) {
        map['pairMap'] = pair;
      }

      var str = JSON.stringify(map);
      weexFunction.setConfig('signal-data-default', str);
    },

    /********************************** 点击*/
    //交易对点击
    pairClick() {

      var counterCoin = this.pairMap ? this.pairMap.counterCoin : '';
      var baseCoin = this.pairMap ? this.pairMap.baseCoin : '';
      var param = {'exchangeId':this.exchangeMap.exchangeId,
                  'exchangeName':this.exchangeMap.exchangeName,
                  counterCoin,
                  baseCoin};
      var self = this;
      var url = this.pairType==0 ? 'up/SelectPairNorm' : 'upContract/CSelectPair';
      var sub = this.pairType==0 ? 'SelectPairNorm' : 'CSelectPair';
      jnavigator.open('', url, param);
      jbroadcast.subscribeOnce(sub, (pair)=>{
        self.pairChange(pair);
      });
    },
    
    //触发类型-改变
    paramTypeChange() {
      this.initDatas();
    },

    //创建信号
    addClick() {
      
      var name = this.name.replace(/\s/g, '');
      if (!name) {
        jmodal.toast(T('请输入信号名称'));
        return;
      }

      if (!this.pairMap) {
        jmodal.toast(T('请选择交易对'));
        return;
      }

      //检查参数
      for (let index = 0; index < this.datas.length; index++) {

        var data = this.datas[index];
        if (data.option) {
          continue;
        }

        var value = data.value.replace(/\s/g, '');
        if (!value) {
          jmodal.toast(T('请输入'+data.name));
          return;
        }
      }

      this.httpsCreateSignal();
    }
  }
}
</script>

<style src="@/_css/style.css" ></style>
<style scoped>
.scroller {
  flex: 1;
}
.wrapper{
  padding-left: 40px;
  padding-right: 40px;
  padding-bottom: 40px;
}
.cell {
  margin-top: 40px;
}
/*************** 启动*/
.up{
  flex-direction: column;
  height: 164px;

  padding-left: 40px;
  padding-right: 40px;
}
.up-but{
  flex-direction: row;
  justify-content: center;
  align-items: center;
  height: 80px;
  border-radius: 4px;

  margin-top: 28px;
}
</style>